//
//  ViewController.m
//  IntellDev_new
//
//  Created by os on 2021/3/5.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

#include "CCTCPDataDefine.h"
#include "CCTCPSocketClient.h"

#import "OpenGLFrameView.h"
#import "AAPLEAGLLayer.h"

@interface ViewController () <TCPSocketClientDelegate>

@property (nonatomic, strong) UIActivityIndicatorView *activityView;

@property (nonatomic, strong) OpenGLFrameView *openGLESView;

@property (nonatomic, strong) AAPLEAGLLayer *displayLayer;

@property (nonatomic, strong) AVSampleBufferDisplayLayer *videoLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.openGLESView = [[OpenGLFrameView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
    self.openGLESView.center = self.view.center;
    [self.view addSubview:self.openGLESView];

    self.activityView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    self.activityView.center = self.view.center;
    self.activityView.hidesWhenStopped = YES;
    self.activityView.userInteractionEnabled = NO;
    [self.openGLESView addSubview:self.activityView];
    
//    self.videoLayer = [[AVSampleBufferDisplayLayer alloc] init];
//    self.videoLayer.frame = CGRectMake(0, 0, 320, 180);
//    self.videoLayer.position = self.view.center;
//    self.videoLayer.opaque = YES;
//    self.videoLayer.videoGravity = AVLayerVideoGravityResizeAspect;
//    self.videoLayer.backgroundColor = UIColor.blackColor.CGColor;
//
//    CMTimebaseRef controlTimebase;
//    CMTimebaseCreateWithMasterClock(CFAllocatorGetDefault(), CMClockGetHostTimeClock(), &controlTimebase);
//    self.videoLayer.controlTimebase = controlTimebase;
//
//    CMTimebaseSetTime(self.videoLayer.controlTimebase, kCMTimeZero);
//    CMTimebaseSetRate(self.videoLayer.controlTimebase, 1.0);
//
//    [self.view.layer addSublayer:self.videoLayer];
        
//    self.displayLayer = [[AAPLEAGLLayer alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
//    self.displayLayer.position = self.view.center;
//    self.displayLayer.backgroundColor = UIColor.blackColor.CGColor;
//    self.displayLayer.opaque = YES;
//    [self.view.layer addSublayer:self.displayLayer];
    
    char* serverIP="192.168.169.148";
    char* userName="zhanlong";
    char* password="12345678";
    
    CC_NetConnectInfo connectionInfo;
    memcpy(connectionInfo.server_ip, serverIP, sizeof(connectionInfo.server_ip));
    memcpy(connectionInfo.user_name, userName, sizeof(connectionInfo.user_name));
    memcpy(connectionInfo.pass_word, password, sizeof(connectionInfo.pass_word));
    connectionInfo.port=30000;
    
    CCTCPSocketClient *socketClient = [[CCTCPSocketClient alloc] init];
    socketClient.tcpSocketDelegate = self;
    [socketClient startTCPSocketConnection:&connectionInfo];
    
//    [self.activityView startAnimating];
}

#pragma mark - TCPSocketClientDelegate

- (void)cameraUpdateDecodedH264FrameData:(H264YUV_Frame *)yuvFrame {
    
    if ([self.activityView isAnimating]) {
        [self.activityView stopAnimating];
    }
    [self.openGLESView render:yuvFrame];
}

- (void)cameraUpdateDecodedH264SampleBuffer:(CMSampleBufferRef)sampleBuffer {
    if (sampleBuffer && [self.videoLayer isReadyForMoreMediaData]) {
        dispatch_async(dispatch_get_main_queue(),^{
            [self.videoLayer enqueueSampleBuffer:sampleBuffer];
        });
    }
}

- (void)cameraUpdateDecodedH264PixelBuffer:(CVPixelBufferRef)imageBuffer {
    if (imageBuffer) {
        self.displayLayer.pixelBuffer = imageBuffer;
    }
}

@end
